import { createTreeNode, createTreeNodeFormArray } from './createTreeNode'
import { CreateTreeNodeOption, CreateTreeNodeOptionFormList, TreeCache } from './interface'
import { VoidTree } from './void-tree'
export * from './interface'

/**
 * 创建数据模型
 * @param options
 */
export function createVoidTree<Raw extends Record<string, any>>(
  options: CreateTreeNodeOption<Raw>
) {
  const { customCacheInit = {} } = options
  const cache: TreeCache<Raw> = { key: new Map() }
  Object.keys(customCacheInit).forEach((key) => {
    cache[key] = customCacheInit[key]()
  })
  const tree = createTreeNode(options, cache)
  return new VoidTree(tree, cache)
}

/**
 * 通过单层数组创建数据模型
 * @param options
 */
export const createVoidTreeByList = <Raw extends Record<string, any>>(
  options: CreateTreeNodeOptionFormList<Raw>
) => {
  const { customCache = {} } = options
  const cache: TreeCache<Raw> = { key: new Map() }
  Object.keys(customCache).forEach((key) => {
    cache[key] = new Map()
  })
  const tree = createTreeNodeFormArray(options, cache)
  return new VoidTree(tree, cache)
}

export const createInitVoidTree = <Raw extends Record<string, any>>() =>
  createVoidTree<Raw>({ raw: [] })
